package com.manage.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.common.util.AJAXResultMessage;
import com.manage.dao.TrainingCourseRepository;
import com.manage.dao.TrainingRecordRepository;
import com.manage.entity.TrainingCourse;
import com.manage.entity.TrainingRecord;
import com.staff.dao.StaffRepository;
import com.staff.entity.Staff;

@Service
@Transactional
public class TrainingRecordService implements ITrainingRecordService{
	
	@Autowired
	private TrainingRecordRepository trainingRecordRepository;
	@Autowired
	private TrainingCourseRepository trainingCourseRepository;
	@Autowired
	private StaffRepository staffRepository;
	
	
	public AJAXResultMessage saveOrUpdate(TrainingRecord trainingRecord) {
		Long staffId=trainingRecord.getStaffId();
		Long trainingCourseId=trainingRecord.getTrainingCourseId();
		if(staffId!=null&&trainingCourseId!=null) {
			TrainingRecord tr=trainingRecordRepository.findRecordByStaffIdAndTrainingCourseId(staffId,trainingCourseId);
			if(tr!=null) {
				return new AJAXResultMessage(false,"不能重复参与");
			}
			Staff staff=staffRepository.findOne(staffId);
			trainingRecord.setStaff(staff);
			TrainingCourse trainingCourse=trainingCourseRepository.findOne(trainingCourseId);
			trainingRecord.setTrainingCourse(trainingCourse);
			trainingRecordRepository.save(trainingRecord);
			return new AJAXResultMessage(true,"你已成功参加培训课程！");
		}
		else {
			return new AJAXResultMessage(false,"用户未空或没选培训项目");
		}

		
	}
	
	public void delete(Long id) {
		TrainingRecord trainingRecord=trainingRecordRepository.findOne(id);
		trainingRecord.setStaff(null);
		trainingRecord.setTrainingCourse(null);
		trainingRecordRepository.save(trainingRecord);
		trainingRecordRepository.delete(trainingRecord);
	}
	
	public void delete(List<Long> ids) {
		List<TrainingRecord> trainingRecordList =  (List<TrainingRecord>) trainingRecordRepository.findAll(ids);
		if(trainingRecordList!=null) {
			for(TrainingRecord trainingRecord :trainingRecordList) {
				trainingRecord.setStaff(null);
				trainingRecord.setTrainingCourse(null);
				trainingRecordRepository.save(trainingRecord);
			}
			trainingRecordRepository.delete(trainingRecordList);
		}
	}
	
	@Transactional(readOnly = true)
	public TrainingRecord findOne(Long id) {
		return trainingRecordRepository.findOne(id);
	}
	
	@Transactional(readOnly = true)
	public List<TrainingRecord> findAll(){
		return (List<TrainingRecord>) trainingRecordRepository.findAll();
	}
	
	@Transactional(readOnly = true)
	public Page<TrainingRecord> findAll(TrainingRecord trainingRecord, Pageable pageable){
		if(trainingRecord.getStaffName()!=null) {
			List<Staff> staffList=staffRepository.findStaffByName(trainingRecord.getStaffName());
			if(staffList.size()>=1) {
				trainingRecord.setStaff(staffList.get(0));
			}				
		}
		if(trainingRecord.getTrainingCourseName()!=null) {
			List<TrainingCourse> tcList=trainingCourseRepository.findTrainingCourseByName(trainingRecord.getTrainingCourseName());
			if(tcList.size()>=1) {
				trainingRecord.setTrainingCourse(tcList.get(0));
			}			
		}
		Page<TrainingRecord> page=trainingRecordRepository.findAll(TrainingRecord.getSpecification(trainingRecord), pageable);;
		List<TrainingRecord> list=page.getContent();
		for(int i=0;i<list.size();i++) {
			Long staffId= list.get(i).getStaff().getId();
			String staffName=list.get(i).getStaff().getStaffName();
			Long trainingCourseId=list.get(i).getTrainingCourse().getId();	
			String trainingCourseName=list.get(i).getTrainingCourse().getTrainName();
			list.get(i).setStaffId(staffId);
			list.get(i).setStaffName(staffName);
			list.get(i).setTrainingCourseId(trainingCourseId);
			list.get(i).setTrainingCourseName(trainingCourseName);
			int day=0;
			if(list.get(i).getTrainingCourse().getStartTime()!=null&&list.get(i).getTrainingCourse().getEndTime()!=null) {
				long a=list.get(i).getTrainingCourse().getEndTime().getTime();
				long b=list.get(i).getTrainingCourse().getStartTime().getTime();
				day=(int) ((a-b)/(24*60*60*1000));
			}
			if(day<=0) {
				day=0;
			}
			list.get(i).setDay(day);

		}
		Page<TrainingRecord> page1 = new PageImpl<TrainingRecord>(list,pageable, page.getTotalElements());
		return page1;
	}


}
